package com.nondo.rdp.finance.service.impl.course;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.Predicate;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.rdp.auth.domain.Role;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.core.domain.attach.Attach;
import com.nondo.rdp.finance.domain.course.Course;
import com.nondo.rdp.finance.domain.course.CourseClassify;
import com.nondo.rdp.finance.domain.course.Section;
import com.nondo.rdp.finance.domain.course.StageCourse;
import com.nondo.rdp.finance.model.course.AdminStageCourseQuery;
import com.nondo.rdp.finance.model.course.AppSectionQuery;
import com.nondo.rdp.finance.model.course.AppStageCourseQuery;
import com.nondo.rdp.finance.repository.course.StageCourseRepository;
import com.nondo.rdp.finance.service.course.CourseClassifyService;
import com.nondo.rdp.finance.service.course.SectionService;
import com.nondo.rdp.finance.service.course.StageCourseService;

@Transactional
@Service
public class StageCourseServiceImpl implements StageCourseService {
	
	private static final int LIMIT_NUM = 4;
	
    @Autowired
    StageCourseRepository stageCourseRepository;
    
    @Autowired
    CourseClassifyService courseClassifyService;
    
    @Autowired
    SectionService sectionService;

    @Override
    public StageCourse findOne(Long id) {
        return this.stageCourseRepository.findOne(id);
    }

    @Override
    public StageCourse save(StageCourse StageCourse) {
        return this.stageCourseRepository.save(StageCourse);
    }

    @Override
    public void update(StageCourse StageCourse) {
        this.stageCourseRepository.save(StageCourse);
    }

    @Override
    public void delete(Long id) {
        this.stageCourseRepository.deleteById(id);
    }

    @Override
    public Page<StageCourse> findAll(Page<StageCourse> page, StageCourse stageCourse) {
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        PageRequest pageRequest = new PageRequest(page.getPageNumber(), page.getPageSize(), sort);
        org.springframework.data.domain.Page<StageCourse> dictPage = stageCourseRepository.findAll((root, query, builder) -> {

            List<Predicate> predicates = new ArrayList<>();
            //搜索ID
            if (stageCourse.getId() != null && stageCourse.getId() != 0) {
            	predicates.add(builder.equal(root.get("id"), stageCourse.getId()));
            }
            //搜索课程名称
            if (StringUtils.isNotEmpty(stageCourse.getName())) {
                predicates.add(builder.like(root.get("course").get("name"), "%" + stageCourse.getName() + "%"));
            }

            predicates.add(builder.like(root.get("delFlag"), Role.DEL_FLAG_NORMAL));
            Predicate[] predicatesArray = new Predicate[predicates.size()];
            predicates.toArray(predicatesArray);
            return builder.and(predicatesArray);

        }, pageRequest);

        return new Page<StageCourse>(dictPage);
    }

	@Override
	public AdminStageCourseQuery getStageCourseQuery(StageCourse stageCourse) {
		AdminStageCourseQuery stageCourseQuery = new AdminStageCourseQuery();
		stageCourseQuery.setId(stageCourse.getId());
		stageCourseQuery.setType(stageCourse.getType());
		Course course = stageCourse.getCourse();
		if ( course.getPclassifyId() != null) {
			Long pclassifyId = Long.valueOf((course.getPclassifyId()));
			CourseClassify pcourseClassify = courseClassifyService.findOne(pclassifyId);
			if (pcourseClassify != null) {
				String pclassifyName = pcourseClassify.getName();
				stageCourseQuery.setPclassifyName(pclassifyName);
			}
		}
		if( course.getClassifyId( )!= null) {
			Long classifyId = Long.valueOf((course.getClassifyId()));
			CourseClassify courseClassify = courseClassifyService.findOne(classifyId);
			if (courseClassify != null) {
				String classifyName = courseClassify.getName();
				stageCourseQuery.setClassifyName(classifyName);
			}
		}
		stageCourseQuery.setCourseName(course.getName());
		Attach attach = course.getMainImg();
		stageCourseQuery.setMainImg(attach.getPath());
		return stageCourseQuery;
	}

	@Override
	public List<AppStageCourseQuery> findAll(Integer type) {
		List<AppStageCourseQuery> stagetCourseQueryList = new ArrayList<AppStageCourseQuery>();
		List<StageCourse> courseQueryList = stageCourseRepository.findAll(type);
		for(StageCourse stageCourse : courseQueryList) {
			AppStageCourseQuery stagetCourseQuery = new AppStageCourseQuery();
			Course course = stageCourse.getCourse();
			stagetCourseQuery.setId(stageCourse.getId());
			stagetCourseQuery.setCid(course.getId());
			stagetCourseQuery.setCourseName(course.getName());
			Attach courseCover = course.getMainImg();
			stagetCourseQuery.setMainImg(courseCover.getPath());
			List<AppSectionQuery> sectionQueryList = new ArrayList<AppSectionQuery>();
			List<Section> sectionList = sectionService.findByCourseId(course.getId());
			if (CollectionUtils.isNotEmpty(sectionList)) {
				int num = 0;
				for (Section section : sectionList) {
					if (num < LIMIT_NUM) {
						AppSectionQuery sectionQuery = new AppSectionQuery();
						sectionQuery.setSid(section.getId());
						sectionQuery.setName(section.getName());
						Attach videoCover = section.getMainImg();
						sectionQuery.setMainImg(videoCover.getPath());
						Attach video = section.getAttach();
						sectionQuery.setVideoId(video.getVideoId());
						sectionQueryList.add(sectionQuery);
					}
					num++;
				}
				stagetCourseQuery.setSectionList(sectionQueryList);
			}
			stagetCourseQueryList.add(stagetCourseQuery);
		}
		return stagetCourseQueryList;
	}

	@Override
	public StageCourse getOne(Long cid, int type) {
		return stageCourseRepository.getOne(cid, type);
	}

}